[contents] [prev] [next] [top] [bottom] (3 out of 5)

Comparing Objects

Chapter 2, "ScriptX Building Blocks," describes a set of operators that are used to compare objects for equality, identity, and magnitude. That chapter also notes that those operators are shorthand for a set of equivalent global functions, which are summarized on page 47. This section summarizes several additional ScriptX comparison functions.

These comparison functions are part of a suite of object comparison functions that ScriptX provides. Because they are based on generic functions, you can override the generics in the classes and objects you define to extend the object Comparison protocol.

Chapter 6, "Defining Classes and Objects," describes how to specialize your classes to allow for comparison of objects.

For more information on the Comparison protocol, see the "Object System Kernel" chapter of the ScriptX Components Guide. For complete definitions of ScriptX comparison functions, see the "Global Functions" chapter of the ScriptX Class Reference.

isComparable and cmp

Two functions that are useful for comparing objects are the generic function isComparable and the cmp global function. The isComparable generic function, defined by RootObject, is one of the four generics that are the basis of the Comparison protocol. isComparable returns true or false, depending on whether its arguments are comparable. If isComparable returns true, then comparison operators and comparison functions (described in Chapter 2, "ScriptX Building Blocks") can be used on the tested objects without causing an exception.

Each class can override isComparable to indicate which classes it can be compared with. The default implementation of isComparable simply states that two objects are comparable if they are of the same class.

isComparable 1 2
true
isComparable 1 "banana"
false

The cmp global function is a general purpose comparison function. When it is called with two arguments (which must be comparable), it uses the equal ( = ) and lt ( < ) generic functions to return one of three values:

The following examples demonstrate the cmp function:

cmp 1 1
@same
cmp 1 4
@before
cmp 4 1
@after

Universal Comparison

In addition to simple comparison tests, ScriptX also defines a set of global functions for determining universal equality and magnitude. The universal comparison functions provide an ordering for all objects, even those that may not be comparable. These functions can be used to sort any objects.

Universal comparison functions can compare two objects of different classes. If two objects are of the same class, and that class does not implement a method for localLT, then ult can still report an exception. Generally, all objects for which comparison is meaningful, such as numbers and strings, implement a method for localLT. To insure that the universal comparison functions work for a given object, use canObjectDo to query the object to see whether it implements localLT. For information on canObjectDo, see page 72.

The seven universal comparison functions are ueq, une, ult, ugt, uge, ule, and ucmp.

The functions ueq (universal equal) and une (universal not equal) test for object equality. The ueq function returns true only if:

Conversely, une returns true if either the classes of its arguments are not the same or the values of its arguments are not the same.

arg1 := 1.9 -- an instance of ImmediateFloat 
arg2 := 1.9 as Float
arg1 = arg2 -- equivalent to the equal global function
true
ueq arg1 arg2 -- test for universal equality
false

In this example, arg1 is assigned the number 1.9 (an instance of class ImmediateFloat), and arg2 is assigned the same value, coerced to the Float class. Because the values of these numbers are the same, the equality test returns true. However, because the classes of these objects are different, ueq returns false.

The global functions ugt, ult, uge, and ule are used for universal comparisons of magnitude. (They are the universal counterparts of the > (gt), < (lt), >= (ge), and <= (le) operations, respectively). Each of these functions returns true if one of the following rules is true:

For example, for the expression ult 1 2, the classes are the same (ImmediateInteger), and the value 1 is indeed less than 2, so the function returns true. Since lt 1 2 returns true, ult 1 2 returns true.
For example, for the expression ult 1 "this", the two classes (ImmediateInteger and String) are different, so the names of the classes are compared. Because ImmediateInteger is alphabetically "less than" String (that is, "I" comes before "S"), the expression returns true.
Universal comparison functions exist to provide a total ordering, which is not necessarily a meaningful ordering, like that provided by their non-universal counterparts. Sorting objects alphabetically by their class name may not be very meaningful, but it does allow objects of any class to be sorted together.

Finally, the ucmp function is the universal equivalent of the cmp function. ucmp returns one of the following values, based on the following rules:


This document is part of the ScriptX Language Guide, one of the volumes of the ScriptX Technical Reference Series. ScriptX is developed by the ScriptX Engineering Team at Apple Computer, successor to the Kaleida Engineering Team at Kaleida Labs, Inc.

Copyright 1996 Apple Computer, Inc. All Rights Reserved.